package com.chinaso.kotlinprojcet.ui

import android.content.Intent
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.widget.Button
import com.chinaso.kotlinprojcet.DataLoader
import com.chinaso.kotlinprojcet.NewsAdapter
import com.chinaso.kotlinprojcet.R
import kotlinx.android.synthetic.main.activity_recycler.*
import org.jetbrains.anko.act
import org.jetbrains.anko.doAsync
import org.jetbrains.anko.uiThread

/**
 * Created by Administrator on 17-11-24.
 */
class RecyclerActivity : AppCompatActivity() {
    val url = "1/news/list?id=all&page=4"
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_recycler)

        initView()
    }

    private fun initView() {
        val btn = findViewById<Button>(R.id.backBtn) as Button
        btn.setOnClickListener { finish() }

        val newsRecycler = findViewById<RecyclerView>(R.id.newsRecycler) as RecyclerView
        newsRecycler.layoutManager = LinearLayoutManager(this)
        getNewsData()
    }

    fun getNewsData() = doAsync {
        val news = DataLoader().getGankNewsList(url)
        uiThread {
            newsRecycler.adapter = NewsAdapter(news) {
                val intent = Intent()
                intent.setClass(act, WebActivity::class.java)
                intent.putExtra("url", it.url)
                startActivity(intent)
            }
        }
    }
}